package Servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DanhMucDAO;
import POJO.DanhMuc;

/**
 * Servlet implementation class AddDanhMucSanPhamServlet
 */
// @WebServlet("/AddDanhMucSanPhamServlet")
public class AddDanhMucSanPhamServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddDanhMucSanPhamServlet()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        RequestDispatcher dispatcher = request
                .getRequestDispatcher("/Add/AddDanhMuc.jsp");
        dispatcher.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        DanhMuc danhMuc = new DanhMuc();
        if (request.getParameterValues("themDanhMuc") != null)
        {
            String[] strs = request.getParameterValues("themDanhMuc");

            // Chuyển sang chữ có dấu
            strs[0] = new String(strs[0].getBytes("ISO-8859-1"), "UTF-8");
            // Lấy tên
            danhMuc.setTenDanhMuc(strs[0]);

            // Lấy ngày tháng năm
            java.util.Date d = new java.util.Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
            String date = dateFormat.format(d);
            danhMuc.setNgayTao(date);

            // Lấy mô tả
            strs[1] = new String(strs[1].getBytes("ISO-8859-1"), "UTF-8");
            danhMuc.setMoTa(strs[1]);

            // Lấy số sản phẩm đang bán
            danhMuc.setSoSanPhamDangBan(Integer.parseInt(strs[2]));

            // Lấy tình trạng
            strs[3] = new String(strs[3].getBytes("ISO-8859-1"), "UTF-8");
            danhMuc.setTinhTrang(strs[3]);

            // Thêm danh mục
            DanhMucDAO.InsertDanhMuc(danhMuc);

            request.setAttribute("id", "1");
            ArrayList<DanhMuc> list = DanhMucDAO.SelectDanhMucALL();

            request.setAttribute("listDanhMuc", list);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
            dispatcher.forward(request, response);
        }
    }
}
